package com.auto.survey.activity.config

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.auto.survey.activity.config.dao.ConfigRepository
import com.auto.survey.activity.config.entity.ConfigBean
import com.auto.survey.activity.config.entity.ConfigWithPoints
import com.auto.survey.activity.config.entity.PointConfig
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class ConfigViewModle :ViewModel() {

    val bean = MutableLiveData<ConfigWithPoints?>()
    val points = MutableLiveData<MutableList<PointConfig>?>()
    val station = MutableLiveData<ConfigBean?>()


    fun getConfigDataByDao(repository: ConfigRepository) {
        viewModelScope.launch {
            val result = withContext(Dispatchers.IO){
                repository.getAllConfigsWithPoints()
            }

            bean.value = result?.get(0)
        }
    }

    /**
     * 测点更新数据
     */
    fun updateConfigDataByPoint(pointConfigs:MutableList<PointConfig>?) {
        viewModelScope.launch {
            val result = withContext(Dispatchers.IO){
                pointConfigs
            }

            points.value = result
        }
    }

    /**
     * 测站更新数据
     */
    fun updateConfigDataStation(config_rlt: ConfigBean?) {
        viewModelScope.launch {
            val result = withContext(Dispatchers.IO){
                config_rlt
            }

            station.value = result
        }
    }

}